//
//  RGMessageBaseTableVC.m
//  RedGrass
//
//  Created by wuyine on 2018/12/10.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMessageBaseTableVC.h"
#import "UIScrollView+EmptyDataSet.h"
#import <MJRefresh.h>

@interface RGMessageBaseTableVC ()<DZNEmptyDataSetDelegate,DZNEmptyDataSetSource,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) MJRefreshHeader *mjHeader;
@property (nonatomic,strong) MJRefreshFooter *mjFooter;
@property (nonatomic,assign) NSInteger pageNum;
@end

@implementation RGMessageBaseTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showBackButton];
    self.customContentView.backgroundColor = [UIColor blackColor];
    [self.customContentView addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    [_mjHeader beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)loadFirstPage {
    self.pageNum = 1;
    HttpRequest *quest = [HttpRequest buildRequest:url_messageList];
    NSDictionary *params = [RGMessageInterfaceBulider bulidMessageListWithType:self.type pageNumber:self.pageNum pageSize:PageSize];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf.mjHeader endRefreshing];
        weakSelf.tableView.emptyDataSetDelegate = self;
        weakSelf.tableView.emptyDataSetSource = self;
        weakSelf.dataList = [RGMessageContactModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        [weakSelf.tableView reloadData];
        
        if (weakSelf.dataList.count < PageSize) {
            weakSelf.mjFooter.state = MJRefreshStateNoMoreData;
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.mjHeader endRefreshing];
        [weakSelf showTextMessageWithMessage:message];
    }];
}

- (void)loadNextPage {
    self.pageNum += 1;
    HttpRequest *quest = [HttpRequest buildRequest:url_messageList];
    NSDictionary *params = [RGMessageInterfaceBulider bulidMessageListWithType:self.type pageNumber:self.pageNum pageSize:PageSize];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
        [weakSelf.mjFooter endRefreshing];
        NSArray *list = [RGMessageContactModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        if (list) {
            [weakSelf.dataList addObjectsFromArray:list];
            [weakSelf.tableView reloadData];
        }
        if (list.count < PageSize) {
            [weakSelf.mjFooter endRefreshingWithNoMoreData];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.mjFooter endRefreshing];
        [weakSelf showTextMessageWithMessage:message];
    }];
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
        _tableView.mj_header = self.mjHeader;
        _tableView.mj_footer = self.mjFooter;
    }
    return _tableView;
}

- (MJRefreshHeader *)mjHeader {
    if (!_mjHeader) {
        _mjHeader = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadFirstPage)];
    }
    return _mjHeader;
}

- (MJRefreshFooter *)mjFooter {
    if (!_mjFooter) {
        _mjFooter = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNextPage)];
    }
    return _mjFooter;
}

- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return UICOLOR_WITH_RGBINT(0x000000);
}

- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    UIFont *font = [UIFont systemFontOfSize:17];
    NSDictionary *attribute = @{NSFontAttributeName:font,
                                NSForegroundColorAttributeName:UICOLOR_WITH_RGBINT(0xffffff),
                                };
    NSAttributedString *title = [[NSAttributedString alloc] initWithString:self.emptyTitle attributes:attribute];
    return title;
}

- (nullable NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    UIFont *font = [UIFont systemFontOfSize:14];
    NSDictionary *attribute = @{NSFontAttributeName:font,
                                NSForegroundColorAttributeName:UICOLOR_WITH_RGBINT(0xdddddd),
                                };
    NSAttributedString *description = [[NSAttributedString alloc] initWithString:self.emptyDescription attributes:attribute];
    return description;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray new];
    }
    return _dataList;
}

@end
